home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2001 December / pcwk12201b.iso / Wersje pelne i specjalne / Winamp 2.77 i 3.0beta / wasabi-sdk_beta1.exe / studio / common / freelist.cpp < prev    next >
C/C++ Source or Header  |  2001-10-08  |  3KB  |  105 lines

  1. /*
  2.  
  3.   Nullsoft WASABI Source File License
  4.  
  5.   Copyright 1999-2001 Nullsoft, Inc.
  6.  
  7.     This software is provided 'as-is', without any express or implied
  8.     warranty.  In no event will the authors be held liable for any damages
  9.     arising from the use of this software.
  10.  
  11.     Permission is granted to anyone to use this software for any purpose,
  12.     including commercial applications, and to alter it and redistribute it
  13.     freely, subject to the following restrictions:
  14.  
  15.     1. The origin of this software must not be misrepresented; you must not
  16.        claim that you wrote the original software. If you use this software
  17.        in a product, an acknowledgment in the product documentation would be
  18.        appreciated but is not required.
  19.     2. Altered source versions must be plainly marked as such, and must not be
  20.        misrepresented as being the original software.
  21.     3. This notice may not be removed or altered from any source distribution.
  22.  
  23.  
  24.   Brennan Underwood
  25.   brennan@nullsoft.com
  26.  
  27. */
  28.  
  29.  
  30. #include "freelist.h"
  31.  
  32. //#define FUCT
  33.  
  34. FreelistPriv::FreelistPriv() {
  35.   total_allocated = 0;
  36. }
  37.  
  38. FreelistPriv::~FreelistPriv() {
  39.   ASSERTPR(total_allocated == 0, "didn't free entire freelist!(1)");
  40.   ASSERTPR(blocks.getNumItems() == 0, "didn't free entire freelist!(2)");
  41. }
  42.  
  43. void *FreelistPriv::getRecord(int typesize, int blocksize, int initialblocksize) {
  44. #ifdef FUCT
  45.   return MALLOC(typesize);
  46. #else
  47.   ASSERT(typesize >= sizeof(void *));
  48.   FLMemBlock *mem = NULL;
  49.   void *freelist = NULL;
  50.   for (int i = 0; i < blocks.getNumItems(); i++) {
  51.     mem = blocks[i];
  52.     if (mem->freelist != NULL) break;
  53.     mem = NULL;
  54.   }
  55.   if (mem == NULL) {
  56.     // figure record count for this new block
  57.     int siz = (blocks.getNumItems() ? blocksize : initialblocksize);
  58.     // allocate another block of memory
  59.     mem = new FLMemBlock(siz*typesize);
  60.     // prelink it into a freelist
  61.     char *record = static_cast<char *>(mem->freelist);
  62.     void **ptr;
  63.     for (int i = 0; i < siz-1; i++) {
  64.       ptr = reinterpret_cast<void **>(record);
  65.       record += typesize;
  66.       *ptr = static_cast<void *>(record);
  67.     }
  68.     // terminate newly made freelist
  69.     ptr = reinterpret_cast<void **>(record);
  70.     *ptr = NULL;
  71.     blocks.addItem(mem);
  72.   }
  73.   // get first free record
  74.   void *ret = mem->freelist;
  75.   // advance freelist *
  76.   mem->freelist = *(static_cast<void **>(mem->freelist));
  77.   mem->nallocated++;
  78.   total_allocated++;
  79.   return ret;
  80. #endif
  81. }
  82.  
  83. void FreelistPriv::freeRecord(void *record) {
  84. #ifdef FUCT
  85.   FREE(record);
  86. #else
  87.   FLMemBlock *mem=NULL;
  88.   for (int i = 0; i < blocks.getNumItems(); i++) {
  89.     mem = blocks[i];
  90.     if (mem->isMine(reinterpret_cast<MBT*>(record))) break;
  91.     mem = NULL;
  92.   }
  93.   ASSERTPR(mem != NULL, "attempted to free record with no block");
  94.   // stash it back on the block's freelist
  95.   *reinterpret_cast<void **>(record) = mem->freelist;
  96.   mem->freelist = record;
  97.   ASSERT(mem->nallocated > 0);
  98.   mem->nallocated--;
  99.   if (mem->nallocated == 0) {
  100.     blocks.delItem(mem);
  101.   }
  102.   total_allocated--;
  103. #endif
  104. }
  105.